package com.huibao.hulibao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.huibao.hulibao.R;
import com.huibao.hulibao.constant.GlobalConstants;
import com.huibao.hulibao.entity.ServerEntity;
import com.huibao.hulibao.utils.PreUtils;
import com.huibao.hulibao.utils.T;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lizhaoyong on 2018/5/7.
 */
public class ServerLvAdapter extends BaseAdapter {
    private ArrayList<ServerEntity.ServerDetailData> mDatas;
    private Context mContext;

    private LayoutInflater mInflater;
    // 判断全部
    private String status;

    public ServerLvAdapter(ArrayList<ServerEntity.ServerDetailData> datas, Context context) {
        this.mDatas = datas;
        this.mContext = context;
        status = (String) PreUtils.getParam(context, "RenWuStatus", "");
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_lv_server, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ServerEntity.ServerDetailData sdd = mDatas.get(position);
        holder.tv_server_lv_title.setText(sdd.ServeName);
        holder.tv_server_lv_room.setText("房间:" + sdd.RoomName);
        holder.tv_server_lv_mom.setText("妈咪:" + sdd.MetherName);
        holder.tv_server_lv_baby.setText("宝宝:" + sdd.BabyName);
        holder.tv_server_lv_data.setText(sdd.ServeTime.substring(5, 10) + "/");
        holder.tv_server_lv_time.setText(sdd.ServeTime.substring(11, 16));

        if (sdd.Status.equals(GlobalConstants.STATUS_NO_FINISH)) {
            holder.tv_server_lv_zhuangtai.setText("未完成");
            holder.iv_server_lv_zhuangtai.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.weiwancheng));
        } else if (sdd.Status.equals(GlobalConstants.STATUS_BUSY)) {
            holder.tv_server_lv_zhuangtai.setText("忙碌");
        } else if (sdd.Status.equals(GlobalConstants.STATUS_NO_CONFIRM)) {
            holder.tv_server_lv_zhuangtai.setText("待确认");
            holder.iv_server_lv_zhuangtai.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.daiqueren));
        } else if (sdd.Status.equals(GlobalConstants.STATUS_NO_DISPATCH)) {
            holder.tv_server_lv_zhuangtai.setText("未分配");
        }

        // 设置图片
        if (sdd.TypeId.equals(GlobalConstants.TYPE_ID_SERVER)) {
            holder.iv_server.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.lv_server_icon));
        } else if (sdd.TypeId.equals(GlobalConstants.TYPE_ID_GOODS)) {
            holder.iv_server.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.lv_server_gift));
        } else if (sdd.TypeId.equals(GlobalConstants.TYPE_ID_DINNER)) {
            holder.iv_server.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.lv_server_dinner));
        } else {
            holder.iv_server.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.lv_server_icon));
        }


        if (status.contains(sdd.TaskId)) {
            holder.tv_server_lv_zhuangtai.setText("执行中");
            holder.tv_server_lv_zhuangtai.setTextColor(mContext.getResources().getColor(R.color.blue));
            holder.iv_server_lv_zhuangtai.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.zhixingzhong));
        } else {
            holder.tv_server_lv_zhuangtai.setTextColor(mContext.getResources().getColor(R.color.text_black));
        }

        return convertView;
    }

    static class ViewHolder {

        @BindView(R.id.iv_server)
        ImageView iv_server;
        @BindView(R.id.tv_server_lv_title)
        TextView tv_server_lv_title;
        @BindView(R.id.tv_server_lv_room)
        TextView tv_server_lv_room;
        @BindView(R.id.tv_server_lv_mom)
        TextView tv_server_lv_mom;
        @BindView(R.id.tv_server_lv_baby)
        TextView tv_server_lv_baby;
        @BindView(R.id.tv_server_lv_zhuangtai)
        TextView tv_server_lv_zhuangtai;
        @BindView(R.id.tv_server_lv_time)
        TextView tv_server_lv_time;
        @BindView(R.id.tv_server_lv_data)
        TextView tv_server_lv_data;
        @BindView(R.id.iv_server_lv_zhuangtai)
        ImageView iv_server_lv_zhuangtai;


        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
